//
//  NewNetWork.m
//  jiaxin
//
//  Created by longziyuan on 14-8-23.
//
//

#import "NewNetWork.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NewNetWork
@synthesize delegate;

-(NSString *)Getmd5code:(NSString *)code
{
    NSString *str = [NSString stringWithFormat:@"%@",code];
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), result );
    return [NSString
            stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1],
            result[2], result[3],
            result[4], result[5],
            result[6], result[7],
            result[8], result[9],
            result[10], result[11],
            result[12], result[13],
            result[14], result[15]
            ];
    
   
    
}









-(void)getNetUrl:(NSString *)prefixStr CanShu:(NSString *)Can
{
    NSURL* URL=[NSURL URLWithString:prefixStr];
    NSMutableURLRequest* request=[NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0f];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[Can dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLConnection* theConnection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (theConnection) {
        NSLog(@"链接成功");
        resultData=[[NSMutableData data]retain];
    }else
    {
        NSLog(@"链接失败");
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [resultData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [resultData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString* theResultStr=[[NSString alloc]initWithBytes:[resultData mutableBytes] length:[resultData length] encoding:NSUTF8StringEncoding];
    if ([theResultStr length]>0) {
        if (delegate) {
            if ([delegate respondsToSelector:@selector(getTheAnalysizeResults:)]) {
                [delegate getTheAnalysizeResults:theResultStr];
            }
        }
    }
    [theResultStr release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
    if (delegate) {
        [delegate getFailResults:@"网络不通或者连接超时"];
    }
    
    [connection cancel];
    [resultData release];
}
@end
